home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / init / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-19  |  11.5 KB  |  447 lines

  1. /*
  2.  *  linux/init/main.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  */
  6.  
  7. #include <stdarg.h>
  8.  
  9. #include <asm/system.h>
  10. #include <asm/io.h>
  11.  
  12. #include <linux/types.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/config.h>
  15. #include <linux/sched.h>
  16. #include <linux/tty.h>
  17. #include <linux/head.h>
  18. #include <linux/unistd.h>
  19. #include <linux/string.h>
  20. #include <linux/timer.h>
  21. #include <linux/fs.h>
  22. #include <linux/ctype.h>
  23. #include <linux/delay.h>
  24. #include <linux/utsname.h>
  25. #include <linux/ioport.h>
  26. #include <linux/bootinfo.h>
  27. #include <linux/mman.h>
  28.  
  29. extern unsigned long * prof_buffer;
  30. extern unsigned long prof_len;
  31. extern char edata, end;
  32. extern char *linux_banner;
  33.  
  34. /*
  35.  * we need this inline - forking from kernel space will result
  36.  * in NO COPY ON WRITE (!!!), until an execve is executed. This
  37.  * is no problem, but for the stack. This is handled by not letting
  38.  * main() use the stack at all after fork(). Thus, no function
  39.  * calls - which means inline code for fork too, as otherwise we
  40.  * would use the stack upon exit from 'fork()'.
  41.  *
  42.  * Actually only pause and fork are needed inline, so that there
  43.  * won't be any messing with the stack from main(), but we define
  44.  * some others too.
  45.  */
  46. #define __NR__exit __NR_exit
  47. static inline _syscall0(int,idle)
  48. static inline _syscall0(int,fork)
  49. static inline _syscall0(int,pause)
  50. static inline _syscall1(int,setup,void *,BIOS)
  51. static inline _syscall0(int,sync)
  52. static inline _syscall0(pid_t,setsid)
  53. static inline _syscall3(int,write,int,fd,const char *,buf,off_t,count)
  54. static inline _syscall1(int,dup,int,fd)
  55. static inline _syscall3(int,execve,const char *,file,char **,argv,char **,envp)
  56. static inline _syscall3(int,open,const char *,file,int,flag,int,mode)
  57. static inline _syscall1(int,close,int,fd)
  58. static inline _syscall1(int,_exit,int,exitcode)
  59. static inline _syscall3(pid_t,waitpid,pid_t,pid,int *,wait_stat,int,options)
  60.  
  61. static inline pid_t wait(int * wait_stat)
  62. {
  63.     return waitpid(-1,wait_stat,0);
  64. }
  65.  
  66. static char printbuf[1024];
  67.  
  68. extern int console_loglevel;
  69.  
  70. extern char empty_zero_page[PAGE_SIZE];
  71. extern int vsprintf(char *,const char *,va_list);
  72. extern void init(void);
  73. extern void config_init(void);
  74. extern void trap_init(void);
  75. extern void init_INTS(void);
  76. extern long kmalloc_init (long,long);
  77. extern long blk_dev_init(long,long);
  78. extern long chr_dev_init(long,long);
  79. extern void floppy_init(void);
  80. extern void sock_init(void);
  81. extern long rd_init(long mem_start, int length);
  82. unsigned long net_dev_init(unsigned long, unsigned long);
  83. extern unsigned long simple_strtoul(const char *,char **,unsigned int);
  84.  
  85. extern void hd_setup(char *str, int *ints);
  86. extern void bmouse_setup(char *str, int *ints);
  87. extern void eth_setup(char *str, int *ints);
  88. extern void xd_setup(char *str, int *ints);
  89. extern void st0x_setup(char *str, int *ints);
  90. extern void tmc8xx_setup(char *str, int *ints);
  91. extern void t128_setup(char *str, int *ints);
  92. extern void generic_NCR5380_setup(char *str, int *intr);
  93. extern void aha152x_setup(char *str, int *ints);
  94.  
  95. #ifdef CONFIG_SYSVIPC
  96. extern void ipc_init(void);
  97. #endif
  98. #ifdef CONFIG_SCSI
  99. extern unsigned long scsi_dev_init(unsigned long, unsigned long);
  100. #endif
  101.  
  102. /*
  103.  * This is set up by the setup-routine at boot-time
  104.  */
  105. #define PARAM    empty_zero_page
  106. #define EXT_MEM_K (*(unsigned short *) (PARAM+2))
  107. #define DRIVE_INFO (*(struct drive_info_struct *) (PARAM+0x80))
  108. #define SCREEN_INFO (*(struct screen_info *) (PARAM+0))
  109. #define MOUNT_ROOT_RDONLY (*(unsigned short *) (PARAM+0x1F2))
  110. #define RAMDISK_SIZE (*(unsigned short *) (PARAM+0x1F8))
  111. #define ORIG_ROOT_DEV (*(unsigned short *) (PARAM+0x1FC))
  112. #define AUX_DEVICE_INFO (*(unsigned char *) (PARAM+0x1FF))
  113.  
  114. /*
  115.  * Boot command-line arguments
  116.  */
  117. #define MAX_INIT_ARGS 8
  118. #define MAX_INIT_ENVS 8
  119. #define COMMAND_LINE ((char *) (PARAM+2048))
  120.  
  121. extern void time_init(void);
  122.  
  123. static unsigned long memory_start = 0;    /* After mem_init, stores the */
  124.                     /* amount of free user memory */
  125. static unsigned long memory_end = 0;
  126. static unsigned long low_memory_start = 0;
  127.  
  128. static char term[21];
  129. int rows, cols;
  130.  
  131.  
  132. static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
  133. static char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", term, NULL, };
  134.  
  135. static char * argv_rc[] = { "/bin/sh", NULL };
  136. static char * envp_rc[] = { "HOME=/", term, NULL };
  137.  
  138. static char * argv[] = { "-/bin/sh",NULL };
  139. static char * envp[] = { "HOME=/usr/root", term, NULL };
  140.  
  141. struct drive_info_struct { char dummy[32]; } drive_info;
  142. struct screen_info screen_info;
  143.  
  144.  
  145. unsigned char aux_device_present;
  146. int ramdisk_size;
  147. int root_mountflags = 0;
  148.  
  149.  
  150. static char command_line[80] = { 0, };
  151.  
  152. struct bootinfo boot_info = {0,};
  153.  
  154. char *get_options(char *str, int *ints)
  155. {
  156.     char *cur = str;
  157.     int i=1;
  158.  
  159.     while (cur && isdigit(*cur) && i <= 10) {
  160.         ints[i++] = simple_strtoul(cur,NULL,0);
  161.         if ((cur = strchr(cur,',')) != NULL)
  162.             cur++;
  163.     }
  164.     ints[0] = i-1;
  165.     return(cur);
  166. }
  167.  
  168. struct {
  169.     char *str;
  170.     void (*setup_func)(char *, int *);
  171. } bootsetups[] = {
  172. #ifdef CONFIG_INET
  173.     { "ether=", eth_setup },
  174. #endif
  175. #ifdef CONFIG_BLK_DEV_HD
  176.     { "hd=", hd_setup },
  177. #endif
  178. #ifdef CONFIG_BUSMOUSE
  179.     { "bmouse=", bmouse_setup },
  180. #endif
  181. #ifdef CONFIG_SCSI_SEAGATE
  182.     { "st0x=", st0x_setup },
  183.     { "tmc8xx=", tmc8xx_setup },
  184. #endif
  185. #ifdef CONFIG_SCSI_T128
  186.     { "t128=", t128_setup },
  187. #endif
  188. #ifdef CONFIG_SCSI_GENERIC_NCR5380
  189.     { "ncr5380=", generic_NCR5380_setup },
  190. #endif
  191. #ifdef CONFIG_SCSI_AHA152X
  192.     { "aha152x=", aha152x_setup},
  193. #endif
  194. #ifdef CONFIG_BLK_DEV_XD
  195.     { "xd=", xd_setup },
  196. #endif
  197.     { 0, 0 }
  198. };
  199.  
  200. int checksetup(char *line)
  201. {
  202.     int i = 0;
  203.     int ints[11];
  204.  
  205.     while (bootsetups[i].str) {
  206.         int n = strlen(bootsetups[i].str);
  207.         if (!strncmp(line,bootsetups[i].str,n)) {
  208.             bootsetups[i].setup_func(get_options(line+n,ints), ints);
  209.             return(0);
  210.         }
  211.         i++;
  212.     }
  213.     return(1);
  214. }
  215.  
  216. unsigned long loops_per_sec = 1;
  217.  
  218. static void calibrate_delay(void)
  219. {
  220.     int ticks;
  221.  
  222.     printk("Calibrating delay loop.. ");
  223.     while (loops_per_sec <<= 1) {
  224.         ticks = jiffies;
  225.         __delay(loops_per_sec);
  226.         ticks = jiffies - ticks;
  227.         if (ticks >= HZ) {
  228. #if defined(__mc68000__)
  229.             __asm__("mulul %1,%0\n\tdivul %2,%0"
  230.                 :"=d" (loops_per_sec)
  231.                 :"d" (HZ),
  232.                  "d" (ticks),
  233.                  "0" (loops_per_sec));
  234. #endif
  235.             printk("ok - %lu.%02lu BogoMips (tm)\n",
  236.                 loops_per_sec/500000,
  237.                 (loops_per_sec/5000) % 100);
  238.             return;
  239.         }
  240.     }
  241.     printk("failed\n");
  242. }
  243.  
  244.  
  245. /*
  246.  * This is a simple kernel command line parsing function: it parses
  247.  * the command line, and fills in the arguments/environment to init
  248.  * as appropriate. Any cmd-line option is taken to be an environment
  249.  * variable if it contains the character '='.
  250.  *
  251.  *
  252.  * This routine also checks for options meant for the kernel - currently
  253.  * only the "root=XXXX" option is recognized. These options are not given
  254.  * to init - they are for internal kernel use only.
  255.  */
  256. static void parse_options(char *line)
  257. {
  258.     char *next;
  259.     char *devnames[] = { "hda", "hdb", "sda", "sdb", "sdc", "sdd", "sde", "fd", "xda", "xdb", NULL };
  260.     int devnums[]     = { 0x300, 0x340, 0x800, 0x810, 0x820, 0x830, 0x840, 0x200, 0xC00, 0xC40, 0};
  261.     int args, envs;
  262.  
  263.     if (!*line)
  264.         return;
  265.     args = 0;
  266.     envs = 1;    /* TERM is set to 'console' by default */
  267.     next = line;
  268.     while ((line = next) != NULL) {
  269.         if ((next = strchr(line,' ')) != NULL)
  270.             *next++ = 0;
  271.         /*
  272.          * check for kernel options first..
  273.          */
  274.         if (!strncmp(line,"root=",5)) {
  275.             int n;
  276.             line += 5;
  277.             if (strncmp(line,"/dev/",5)) {
  278.                 ROOT_DEV = simple_strtoul(line,NULL,16);
  279.                 continue;
  280.             }
  281.             line += 5;
  282.             for (n = 0 ; devnames[n] ; n++) {
  283.                 int len = strlen(devnames[n]);
  284.                 if (!strncmp(line,devnames[n],len)) {
  285.                     ROOT_DEV = devnums[n]+simple_strtoul(line+len,NULL,10);
  286.                     break;
  287.                 }
  288.             }
  289.         } else if (!strcmp(line,"ro"))
  290.             root_mountflags |= MS_RDONLY;
  291.         else if (!strcmp(line,"rw"))
  292.             root_mountflags &= ~MS_RDONLY;
  293.         else if (!strcmp(line,"debug"))
  294.             console_loglevel = 10;
  295.         else if (!strcmp(line,"no387")) {
  296.             hard_math = 0;
  297.         } else
  298.             checksetup(line);
  299.         /*
  300.          * Then check if it's an environment variable or
  301.          * an option.
  302.          */
  303.         if (strchr(line,'=')) {
  304.             if (envs >= MAX_INIT_ENVS)
  305.                 break;
  306.             envp_init[++envs] = line;
  307.         } else {
  308.             if (args >= MAX_INIT_ARGS)
  309.                 break;
  310.             argv_init[++args] = line;
  311.         }
  312.     }
  313.     argv_init[args+1] = NULL;
  314.     envp_init[envs+1] = NULL;
  315. }
  316.  
  317. asmlinkage void start_kernel(void)
  318. {
  319. /*
  320.  * Interrupts are still disabled. Do necessary setups, then
  321.  * enable them
  322.  */
  323.     config_init();
  324.     ramdisk_size = boot_info.ramdisk_size;
  325.     strcpy(command_line,boot_info.command_line);
  326.     paging_init (&memory_start, &memory_end);
  327.     trap_init();
  328.     init_INTS();
  329.     sched_init();
  330.     parse_options(command_line);
  331. #ifdef CONFIG_PROFILE
  332.     prof_buffer = (unsigned long *) memory_start;
  333.     prof_len = (unsigned long) &end - 0xC0000000;
  334.     prof_len >>= 2;
  335.     memory_start += prof_len * sizeof(unsigned long);
  336. #endif
  337.     memory_start = kmalloc_init(memory_start,memory_end);
  338.     memory_start = chr_dev_init(memory_start,memory_end);
  339.     memory_start = blk_dev_init(memory_start,memory_end);
  340. #ifdef CONFIG_INET
  341.     memory_start = net_dev_init(memory_start,memory_end);
  342. #endif
  343.     sti();
  344.     calibrate_delay();
  345. #ifdef CONFIG_SCSI
  346.     if(boot_info.bi_atari.model == ATARI_TT)                 /* Cause there are probs with Falcon */
  347.         memory_start = scsi_dev_init(memory_start,memory_end);
  348. #endif
  349.     memory_start = inode_init(memory_start,memory_end);
  350.     memory_start = file_table_init(memory_start,memory_end);
  351.     mem_init(low_memory_start,memory_start,memory_end);
  352.     buffer_init();
  353.     floppy_init();
  354.     time_init();
  355.     sock_init();
  356. #ifdef CONFIG_SYSVIPC
  357.     ipc_init();
  358. #endif
  359.     sti();
  360.  
  361.     printk(linux_banner);
  362.     /* Print the BSD copyright! */
  363.     printk ("This product includes software developed by"
  364.         " the University of\n");
  365.     printk ("California, Berkeley and its contributors.\n");
  366.  
  367.     move_to_user_mode();
  368.     if (!fork())            /* we count on this going ok */
  369.          init();
  370.        
  371. /*
  372.  * task[0] is meant to be used as an "idle" task: it may not sleep, but
  373.  * it might do some general things like count free pages or it could be
  374.  * used to implement a reasonable LRU algorithm for the paging routines:
  375.  * anything that can be useful, but shouldn't take time from the real
  376.  * processes.
  377.  *
  378.  * Right now task[0] just does a infinite idle loop.
  379.  */
  380.     for(;;)
  381.         idle();
  382. }
  383.  
  384. static int printf(const char *fmt, ...)
  385. {
  386.     va_list args;
  387.     int i;
  388.  
  389.     va_start(args, fmt);
  390.     write(1,printbuf,i=vsprintf(printbuf, fmt, args));
  391.     va_end(args);
  392.     return i;
  393. }
  394.  
  395. void init(void)
  396. {
  397.     int pid,i;
  398.  
  399.  
  400.     setup((void *) &drive_info);
  401.     sprintf(term, "TERM=console");
  402.     (void) open("/dev/tty1",O_RDWR,0);
  403.     (void) dup(0);
  404.     (void) dup(0);
  405.  
  406.     execve("/etc/init",argv_init,envp_init);
  407.     execve("/bin/init",argv_init,envp_init);
  408.     execve("/sbin/init",argv_init,envp_init);
  409.     /* if this fails, fall through to original stuff */
  410.  
  411.     if (!(pid=fork())) {
  412.         close(0);
  413.         if (open("/etc/rc",O_RDONLY,0))
  414.             _exit(1);
  415.         execve("/bin/sh",argv_rc,envp_rc);
  416.         _exit(2);
  417.     }
  418.     if (pid>0)
  419.         while (pid != wait(&i))
  420.             /* nothing */;
  421.     while (1) {
  422.         if ((pid = fork()) < 0) {
  423.             printf("Fork failed in init\n\r");
  424.             continue;
  425.         }
  426.         if (!pid) {
  427.             close(0);close(1);close(2);
  428.             setsid();
  429.             (void) open("/dev/tty1",O_RDWR,0);
  430.             (void) dup(0);
  431.             (void) dup(0);
  432.             printf ("execing sh.. ");
  433.             i = execve("/bin/sh",argv,envp);
  434.             i = errno;
  435.             printf ("execve returned %d\n", i);
  436.             _exit(i);
  437.             _exit(execve("/bin/sh",argv,envp));
  438.         }
  439.         while (1)
  440.             if (pid == wait(&i))
  441.                 break;
  442.         printf("\n\rchild %d died with code %04x\n\r",pid,i);
  443.         sync();
  444.     }
  445.     _exit(0);
  446. }
  447.